package com.da.lon.wang.xlg.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.adapter.Classify2Adapter;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultCategory.SearchResultCategoryItems;
import com.da.lon.wang.xlg.bean.ResultSearchBean.ResultSearchListBean.SearchResultOther.SearchResultOtherItems;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class Classify2Activity extends BaseActivity implements OnClickListener {

    public static Classify2Activity ActivityA;
    private Context context;
    private String style;
    private int resultCode;
    private List<SearchResultOtherItems> list = new ArrayList<SearchResultOtherItems>();
    private List<SearchResultCategory> list2 = new ArrayList<SearchResultCategory>();
    private List<SearchResultCategoryItems> list3 = new ArrayList<SearchResultCategoryItems>();
    private ImageButton leftImageButton;
    private ListView listView;
    private Classify2Adapter adapter;
    private int mposition;

    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ouka_classify2);

        context = this;

        ActivityA = this;

        Intent intent = getIntent();
        list2 = (List<SearchResultCategory>) intent.getSerializableExtra("categorylist");
        list = (List<SearchResultOtherItems>) intent.getSerializableExtra("otherlist");
        style = intent.getStringExtra("style");
        mposition = getIntent().getIntExtra("position", 0);
        initView();

    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    private void initView() {
        // TODO Auto-generated method stub

        try {
            leftImageButton = (ImageButton) findViewById(R.id.ouka_classify2_left);
            leftImageButton.setOnClickListener(this);

            listView = (ListView) findViewById(R.id.ouka_classify2_lv);
            adapter = new Classify2Adapter(list, list2, context, style);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    // TODO Auto-generated method stub

                    if (style.equals("1")) {

                        list3.clear();
                        list3.addAll(list2.get(position).getItems());
                        Intent intent = new Intent(Classify2Activity.this, Classify3Activity.class);
                        intent.putExtra("cateitemslist", (Serializable) list3);

                        startActivity(intent);

                    } else {

                        String sattr = PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.SEARCH_SATTR, "");

                        String sattr1 = null;

                        if (sattr.equals("")) {
                            if (list != null && list.get(position) != null) {
                                sattr1 = list.get(position).getName();
                            }
                        } else {
                            if (list != null && list.get(position) != null) {
                                sattr1 = sattr + "," + list.get(position).getName();
                            }
                        }

                        PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.SEARCH_SATTR, sattr1);

                        Intent intent = new Intent();
                        if (list != null && list.get(position) != null) {
                            SearchResultOtherItems otherName = list.get(position);
                            intent.putExtra("otherName", otherName);
                        }
                        intent.putExtra("position", mposition);
                        setResult(resultCode, intent);
                        finish();
                    }
                }
            });

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {

            case R.id.ouka_classify2_left:

                finish();

                break;
        }
    }
}
